package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.service.SearchCustomerService;
import com.xbongbong.paas.service.SearchCustomerDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.searchcustomer.pojo.dto.SearchCustomerDTO;
import com.xbongbong.pro.searchcustomer.pojo.dto.SearchCustomerDataDTO;
import com.xbongbong.pro.searchcustomer.pojo.dto.SearchEnterpriseDTO;
import com.xbongbong.pro.searchcustomer.pojo.vo.JumpClueDetailVO;
import com.xbongbong.pro.searchcustomer.pojo.vo.SearchCustomerDataVO;
import com.xbongbong.pro.searchcustomer.pojo.vo.SearchCustomerMsgVO;
import com.xbongbong.pro.searchcustomer.pojo.vo.SearchCustomerVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author shian
 * @create 2019-08-26 14:11
 * @Description 搜客管理移动端
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/searchCustomer")
public class MobileSearchCustomerController {

    @Resource
    private SearchCustomerService searchCustomerService;
    @Resource
    private SearchCustomerDataService searchCustomerDataService;

    /**
     * 搜客应用开户注册接口
     * @param searchCustomerDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-02
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String register(@RequestBody @Valid SearchCustomerDTO searchCustomerDTO, BindingResult br){
        XbbResponse<SearchCustomerVO> response;
        SearchCustomerVO searchCustomerVO;
        JSONObject jsonObject;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                jsonObject = searchCustomerService.sendRegister(searchCustomerDTO);
                searchCustomerVO = JSONObject.toJavaObject(jsonObject ,SearchCustomerVO.class);
                response = new XbbResponse<>(searchCustomerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 登录搜客应用
     * @param searchCustomerDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-02
     */
    @RequestMapping(value = "/signIn", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String loginSearchCustomer(@RequestBody @Valid SearchCustomerDTO searchCustomerDTO, BindingResult br){
        XbbResponse<SearchCustomerVO> response;
        SearchCustomerVO searchCustomerVO;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                searchCustomerVO = searchCustomerService.loginSearchCustomer(searchCustomerDTO);
                response = new XbbResponse<>(searchCustomerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查询（主/子）账号信息
     * @param searchCustomerDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-05
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userMessage(@RequestBody @Valid SearchCustomerDTO searchCustomerDTO, BindingResult br){
        XbbResponse<SearchCustomerVO> response;
        SearchCustomerVO searchCustomerVO;
        JSONObject jsonObject;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                jsonObject = searchCustomerService.info(searchCustomerDTO);
                searchCustomerVO = JSONObject.toJavaObject(jsonObject ,SearchCustomerVO.class);
                response = new XbbResponse<>(searchCustomerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 前端获取搜客转过来的数据（搜客转客户->后端接收数据->前端拿数据）
     * @param searchCustomerDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-20
     */
    @RequestMapping(value = "/notice/value", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String notice(@RequestBody @Valid SearchCustomerDTO searchCustomerDTO, BindingResult br){
        XbbResponse<SearchCustomerDataVO> response;
        SearchCustomerDataVO searchCustomerDataVO;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                searchCustomerDataVO = searchCustomerService.notice(searchCustomerDTO);
                response = new XbbResponse<>(searchCustomerDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);

    }

    /**
     * 搜客转客户或转公海
     * @param searchCustomerDataDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-13
     */
    @RequestMapping(value = "/transfer/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String transferCustomer(@RequestBody @Valid SearchCustomerDataDTO searchCustomerDataDTO, BindingResult br){
        XbbResponse<SearchCustomerMsgVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(searchCustomerDataDTO.getSaasMark(), searchCustomerDataDTO.getBusinessType(), searchCustomerDataDTO.getMenuId(), searchCustomerDataDTO.getDistributorMark());
                SearchCustomerMsgVO searchCustomerMsgVO = searchCustomerDataService.addBatch(searchCustomerDataDTO);
                response = new XbbResponse<>(searchCustomerMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取转客户或公海的任务状态
     * @param searchCustomerDataDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-09-09
     */
    @RequestMapping(value = "/transfer/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getTransferStatus(@RequestBody @Valid SearchCustomerDataDTO searchCustomerDataDTO, BindingResult br){
        XbbResponse<SearchCustomerMsgVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SearchCustomerMsgVO searchCustomerMsgVO = searchCustomerDataService.getStatus(searchCustomerDataDTO);
                response = new XbbResponse<>(searchCustomerMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 企业模糊查询
     * @param searchEnterpriseDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-15
     */
    @RequestMapping(value = "/search/enterprise", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String search(@RequestBody @Valid SearchEnterpriseDTO searchEnterpriseDTO, BindingResult br){
        XbbResponse<SearchCustomerVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SearchCustomerVO searchCustomerVO = searchCustomerService.search(searchEnterpriseDTO);
                response = new XbbResponse<>(searchCustomerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取企业基本信息
     * @param searchEnterpriseDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2019-08-15
     */
    @RequestMapping(value = "/enterprise/baseInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String baseInfo(@RequestBody @Valid SearchEnterpriseDTO searchEnterpriseDTO, BindingResult br){
        XbbResponse<SearchCustomerVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SearchCustomerVO searchCustomerVO = searchCustomerService.baseInfo(searchEnterpriseDTO);
                response = new XbbResponse<>(searchCustomerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/clue/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getClueDetail(@RequestBody @Valid SearchEnterpriseDTO searchEnterpriseDTO, BindingResult br){
        XbbResponse<JumpClueDetailVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                JumpClueDetailVO jumpClueDetailVO = searchCustomerService.getClueDetail(searchEnterpriseDTO);
                response = new XbbResponse<>(jumpClueDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
